<?php

/**
* The function "gd_info()" is available as of PHP version 4.3.0.
* The redefinition here does essentially the same as the native function.
* 
* @return	mixed		array
* @access	public
* @author	Christian Machmeier
*/
if (!function_exists('gd_info')) {

	function gd_info() {
		$array = array (
			'GD Version' 		 => '',
			'FreeType Support'	 => 0,
			'FreeType Support'	 => 0,
			'FreeType Linkage'	 => '',
			'T1Lib Support'		 => 0,
			'GIF Read Support'	 => 0,
			'GIF Create Support' => 0,
			'JPG Support'		 => 0,
			'PNG Support'		 => 0,
			'WBMP Support'		 => 0,
			'XBM Support'		 => 0
		);
		
		$gif_support = 0;
		
		ob_start();
		phpinfo(8);
		$info = ob_get_contents();
		ob_end_clean();
		
		foreach (explode("\n", $info) as $line) {
			
			if (strpos($line, 'GD Version') 		!== false) {
				$array['GD Version']		 = trim(str_replace('GD Version', '', strip_tags($line)));
			}
			
			if (strpos($line, 'FreeType Support')   !== false) {
				$array['FreeType Support']	 = trim(str_replace('FreeType Support', '', strip_tags($line)));
			}
			
			if (strpos($line, 'FreeType Linkage')   !== false) {
				$array['FreeType Linkage']	 = trim(str_replace('FreeType Linkage', '', strip_tags($line)));
			}
			
			if( strpos($line, 'T1Lib Support') 	   !== false) {
				$array['T1Lib Support']		 = trim(str_replace('T1Lib Support', '', strip_tags($line)));
			}
			
			if (strpos($line, 'GIF Read Support')   !== false) {
				$array['GIF Read Support']   = trim(str_replace('GIF Read Support', '', strip_tags($line)));
			}
			
			if (strpos($line, 'GIF Create Support') !== false) {
				$array['GIF Create Support'] = trim(str_replace('GIF Create Support', '', strip_tags($line)));
			}
			
			if (strpos($line, 'GIF Support') 	   !== false) {
				$gif_support				 = trim(str_replace('GIF Support', '', strip_tags($line)));
			}
			
			if (strpos($line, 'JPG Support') 	   !== false) {
				$array['JPG Support']		 = trim(str_replace('JPG Support', '', strip_tags($line)));
			}
			
			if (strpos($line, 'PNG Support') 	   !== false) {
				$array['PNG Support']		 = trim(str_replace('PNG Support', '', strip_tags($line)));
			}
			
			if (strpos($line, 'WBMP Support') 	   !== false) {
				$array['WBMP Support']		 = trim(str_replace('WBMP Support', '', strip_tags($line)));
			}
			
			if (strpos($line, 'XBM Support') 	   !== false) {
				$array['XBM Support']		 = trim(str_replace('XBM Support', '', strip_tags($line)));
			}
			
		}
		
		if ($gif_support				 === 'enabled') {
			$array['GIF Read Support']   = 1;
			$array['GIF Create Support'] = 1;
		}
		
		if ($array['FreeType Support']	 === 'enabled') {
			$array['FreeType Support'] 	 = 1;
		}
		
		if ($array['T1Lib Support']		 === 'enabled') {
			$array['T1Lib Support']		 = 1;   
		}
		
		if ($array['GIF Read Support']	 === 'enabled') {
			$array['GIF Read Support']	 = 1;
		}
		
		if ($array['GIF Create Support'] === 'enabled') {
			$array['GIF Create Support'] = 1;   
		}
		
		if ($array['JPG Support']		 === 'enabled') {
			$array['JPG Support']		 = 1;
		}
		
		if ($array['PNG Support']		 === 'enabled') {
			$array['PNG Support']		 = 1;
		}
		
		if ($array['WBMP Support']		 === 'enabled') {
			$array['WBMP Support']		 = 1;
		}
		
		if ($array['XBM Support']		 === 'enabled') {
			$array['XBM Support']		 = 1;
		}
		
		return $array;
	}
	
}


/**
* Checks, if the required GD library is available. Returns false on error,
* else returns the value of the installed GD version.
* 
* @return	mixed
* @see		gd_info()
* @see		createImage()
* @access	public
* @author	Christian Machmeier
*/
function checkGD()
{
	$error = false;
	$gd    = array();
	
	if (!extension_loaded('gd')) {
		if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
			if (!dl('php_gd.dll')) {
				$error = true;
			}
		} else {
			if (!dl('gd.so')) {
				$error = true;
			}
		}
	}
	
	if ($error) {
		return false;
	} else {
		$gdInfo		 		  = gd_info();
		$gd['version'] 		  = stristr($gdInfo['GD Version'], '2.') ? 2 : 1;
		$gd['formats']['gif'] = $gdInfo['GIF Read Support'];
		$gd['formats']['jpeg'] = $gdInfo['JPG Support'];
		$gd['formats']['png'] = $gdInfo['PNG Support'];
		
		return $gd;
	}
	
}


/**
* Create a squared thumbnail from an existing image.
* 
* @param	string		$file		Location and name where the file is stored .
* @return	boolean
* @access	public
* @author	Christian Machmeier
*/
function createImage($file, $fileDest, $imageSize=45, $mode='thumbs', $format='jpeg')
{
	// Get information about the installed GD.
	$gdInfo    = checkGD();
	$gdVersion = $gdInfo['version'];
	
	if ($gdInfo == false) {
		return false;
		echo '
			Necessary GD image functions are not available.<br />
			You will waste harddisk capacity due to non thumbnail creation. Your original image is instead simply 
			copied as preview image. But beware: using this inadvisable approach causes	that your image preview 
			within your albums will be very slow.
		';
	}
	
	// Ensure the given format is supported.
	if ($gdInfo['formats'][$format] != 1) {
		//echo 'extension/format non supporte';
		return false;
	}
	
	// Get the image dimensions.
	$dimensions = getimagesize($file);
	$width		= $dimensions[0];
	$height		= $dimensions[1];
	
	// images mode.
	if ($mode == 'images') {
		$deltaX   = 0;
		$deltaY   = 0;
		$portionX = $outputX = $width;
		$portionY = $outputY = $height;
		$quality  = 100;
	}
	
	// thumbs mode.
	if ($mode == 'thumbs') {
		$outputX  = $imageSize;
		$outputY  = $imageSize;
		$imageSize > 100 ? $quality = 100 : $quality = 85;
		
		// The image is of vertical shape.
		if ($width < $height) {
			$deltaX   = 0;
			$deltaY   = ($height - $width) / 2;
			$portionX = $width;
			$portionY = $width;
			
		// The image is of horizontal shape.
		} else if ($width > $height) {
			$deltaX   = ($width - $height) / 2;
			$deltaY   = 0;
			$portionX = $height;
			$portionY = $height;
			
		// The image is of squared shape.
		} else {
			$deltaX   = 0;
			$deltaY   = 0;
			$portionX = $width;
			$portionY = $height;
		}
	}

	// Get the source image in gif format.
	if ($format == 'gif') {
		$imageSrc  = imagecreatefromgif($file);
	}
	
	// Get the source image in jpg format.
	if ($format == 'jpeg') {
		$imageSrc  = imagecreatefromjpeg($file);
	}
	
	// Get the source image in png format.
	if ($format == 'png') {
		$imageSrc  = imagecreatefrompng($file);
	}
	
	// The thumbnail creation with GD1.x functions does the job.
	if ($gdVersion == 1) {
		// Create an empty thumbnail image.
		$imageDest = imagecreate($outputX, $outputY);
		
		// Try to create the thumbnail from the source image.
		if (imagecopyresized($imageDest, $imageSrc, 0, 0, $deltaX, $deltaY, $outputX, $outputY, $portionX, $portionY)) {
			
			// save the thumbnail image into a file.
			imagejpeg($imageDest, $fileDest, $quality);
			
			// Delete both image resources.
			imagedestroy($imageSrc);
			imagedestroy($imageDest);
			
			return true;
			
		}
		
	}
	
	// The recommended approach is the usage of the GD2.x functions.
	if ($gdVersion == 2) {

		// Create an empty thumbnail image.
		$imageDest = imagecreatetruecolor($outputX, $outputY);
		
		// Try to create the thumbnail from the source image.
		if (imagecopyresampled($imageDest, $imageSrc, 0, 0, $deltaX, $deltaY, $outputX, $outputY, $portionX, $portionY)) {
			
			// save the thumbnail image into a file.
			imagejpeg($imageDest, $fileDest, $quality);
			
			// Delete both image resources.
			imagedestroy($imageSrc);
			imagedestroy($imageDest);
			
			return true;
			
		}
		
	}
	
	return false;
}


?>